package com.course.controller;
/*
@author By 林中小鸟
@package  com.self.introduction.controller
@create 2021 - 11 - 2021\11\26 0026
@QQ: 357182695
@Em: themooncss@gmail.com
*/

import com.course.component.enums.ResultStatus;
import com.course.component.exceptions.FileException;
import com.course.component.http.Crp;
import com.course.component.http.requ.RequestFile;
import com.course.service.FilesUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/x")
public class FileController {

    @Autowired
    private FilesUploadService fileService;

    @PostMapping("/uploadShardLocal")
    @ResponseBody
    //@PassToken  //过滤登陆校验
    public Crp uploadLocalShard(RequestFile requestFile) throws Exception {
        Map fileRes = fileService.storeFileShard(requestFile);
        String fileName = (String) fileRes.get("fileName");
        String fileDownloadUri = (String) fileRes.get("file_download_uri");
        Map res = new HashMap();
        res.put("file_name", fileName);
        res.put("file_download_uri", fileDownloadUri);
        res.put("src", fileDownloadUri);
        res.put("size", requestFile.getSize());
        return new Crp(ResultStatus.SUCCESS, res);
    }

    @PostMapping("/upload")
    @ResponseBody
    //@PassToken  //过滤登陆校验
    public Crp upload(@RequestParam("file") MultipartFile file) {
        Map fileRes = fileService.storeFile(file);
        String fileName = (String) fileRes.get("fileName");
        String fileDownloadUri = (String) fileRes.get("file_download_uri");
        Map res = new HashMap();
        res.put("file_name", fileName);
        res.put("file_download_uri", fileDownloadUri);
        res.put("src", fileDownloadUri);
        res.put("type", file.getContentType());
        res.put("size", file.getSize());
        return new Crp(ResultStatus.SUCCESS, res);
    }

    @PostMapping("/uploads")
    @ResponseBody
    //@PassToken  //过滤登陆校验
    public Crp uploads(@RequestParam("files") MultipartFile[] files) {
        List res = Arrays.stream(files).map(fileService::uploadFiles).collect(Collectors.toList());
        return new Crp(ResultStatus.SUCCESS, res);
    }

    @ResponseBody
    //@PassToken  //过滤登陆校验
    @GetMapping("/download/{fileName:.+}")
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
        // Load file as Resource
        Resource resource = fileService.loadFileAsResource(fileName);

        // Try to determine file's content type
        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            throw new FileException(500, "文件丢失", false);
        }

        // Fallback to the default content type if type could not be determined
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }
}
